﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Web;
using System.ComponentModel;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Data.Common;


namespace QiHe
{
    public class DBS
    {
        string connectionString = @"Server=121.42.185.160;Database=cmu;Uid=root;Pwd=123654;Port=3306;";


        public string Get_DateTime()
        {



            DateTime Daytemp = DateTime.Now;
            string year = Daytemp.Year.ToString();
            string month = null;
            string day = null;
            month = Daytemp.Month.ToString();
            day = Daytemp.Day.ToString();
            if (Daytemp.Month.ToString().Length == 1)
                month = "0" + Daytemp.Month.ToString();

            if (Daytemp.Day.ToString().Length == 1)
                day = "0" + Daytemp.Day.ToString();


            return year + "/" + month + "/" + day + " " + DateTime.Now.ToLongTimeString();


        }
        public DataSet Task_Approved_List(string TaskFlag)
        {



            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();




            string selectStr = "select 审批表.ID,审批表.审批意见,审批表.审批附言,审批表.审批时间,审批表.审批人 FROM  审批表   WHERE 审批表.任务Flag= '" + TaskFlag + "'  order by 审批表.ID";


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  




            return ds;


        }

        public string Add_Approve(string Flag, string Approve, string Approvecontent, string ApproveUserID)
        {



            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "insert into 审批表 (任务Flag,审批意见,审批附言,审批人,审批时间) values('" + Flag + "','" + Approve + "','" + Approvecontent + "','" + ApproveUserID + "','" + Get_DateTime() + "')";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();

            selectStr = "update 报修任务表 set 审批意见 ='" + Approve + "',审批附言='" + Approvecontent + "', 审批人='" + ApproveUserID + "' where  Flag= '" + Flag + "'";
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "1";
        }
        public string Add_Apply(string Flag, string ApplyContent, double facprice, double applyprice, string UserID)
        {


            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "update 报修任务表 set 审批意见 ='等待审批',市场价格='" + facprice + "', 维修预算='" + applyprice + "',预算附言='" + ApplyContent + "', 申请人='" + UserID + "'  where  Flag= '" + Flag + "'";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "1";
        }
        public DataSet RPlist_All()
        {
            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();




            string selectStr = "select 设备表.设备名称,报修任务表.故障描述,报修任务表.报修时间,报修任务表.维修进程,User2.ChineseName,Depts.Name,报修任务表.报修人,报修任务表.Flag,Dept2.Name FROM  报修任务表  JOIN Depts  ON 报修任务表.所属科室=Depts.DeptNumber JOIN Depts Dept2 ON  Depts.PID= Dept2.DeptNumber JOIN Users as User2  ON 报修任务表.执行人=User2.ID JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag   WHERE 报修任务表.维修进程!= '任务完成' order by 报修任务表.ID desc";


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();
            ds.Tables[0].Columns[4].ColumnName = "执行人";
            ds.Tables[0].Columns[5].ColumnName = "报修科室";
            ds.Tables[0].Columns[6].ColumnName = "报修人";
            ds.Tables[0].Columns[8].ColumnName = "报修学院";




            return ds;


        }


        public DataSet EQInfo_By_ID(string ID)
        {
            string EQFlag = ID;
            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "select 设备表.设备名称,设备表.设备型号,设备表.出厂编号,设备表.生产厂商,设备表.购入日期,Depts.Name,Depts.DeptNumber,设备表.Flag,设备表.存放位置,Dept2.Name FROM 设备表 JOIN Depts ON  设备表.所属科室= Depts.DeptNumber JOIN Depts Dept2 ON  Depts.PID= Dept2.DeptNumber where Flag= '" + EQFlag + "'"; ;


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();




            return ds;




        }

        public DataSet Dept_List(string PID)
        {

            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();




            string selectStr = "select Name,DeptNumber from Depts  WHERE  PID= '" + PID + "'and Name!='未知部门' and Name!='维修中心'";


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();




            return ds;

        }
        public DataSet RPlist_Finished()
        {


            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select 设备表.设备名称,报修任务表.故障描述,报修任务表.报修时间,报修任务表.维修进程,User2.ChineseName,Depts.Name,报修任务表.报修人,报修任务表.Flag,Dept2.Name FROM  报修任务表  JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber JOIN Depts Dept2 ON  Depts.PID= Dept2.DeptNumber JOIN Users as User2  ON  报修任务表.执行人=User2.ID  JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag   WHERE  报修任务表.维修进程= '任务完成'  order by 报修任务表.ID desc";


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();
            ds.Tables[0].Columns[4].ColumnName = "执行人";
            ds.Tables[0].Columns[5].ColumnName = "报修科室";
            ds.Tables[0].Columns[6].ColumnName = "报修人";
            ds.Tables[0].Columns[8].ColumnName = "报修学院";




            return ds;


        }



       
        public DataSet RPlist_Unreply()
        {


            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select Dept2.Name,Depts.Name,设备表.设备名称,报修任务表.维修进程,User2.ChineseName,报修任务表.报修人,报修任务表.报修时间,报修任务表.Flag,报修任务表.故障描述 FROM  报修任务表  JOIN Depts  ON 报修任务表.所属科室=Depts.DeptNumber JOIN Depts Dept2 ON  Depts.PID= Dept2.DeptNumber JOIN Users as User2  ON  报修任务表.执行人=User2.ID  JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag    WHERE  报修任务表.维修进程= '等待回应'  order by 报修任务表.ID desc";


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();
            ds.Tables[0].Columns[4].ColumnName = "执行人";
            ds.Tables[0].Columns[5].ColumnName = "报修科室";
            ds.Tables[0].Columns[6].ColumnName = "报修人";
            ds.Tables[0].Columns[8].ColumnName = "报修学院";




            return ds;


        }

        public DataSet TaskInfo(string Flag)
        {
            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();



            string selectStr = "select 设备表.设备名称,设备表.设备型号,设备表.出厂编号,设备表.生产厂商,设备表.购入日期,Depts.Name,Depts.DeptNumber,设备表.Flag,设备表.存放位置,Dept2.Name,报修任务表.报修人,报修任务表.报修人电话,报修任务表.报修时间,报修任务表.故障描述,报修任务表.维修进程 FROM 报修任务表  JOIN 设备表 ON  报修任务表.设备Flag= 设备表.Flag JOIN Depts ON 设备表.所属科室= Depts.DeptNumber JOIN Depts Dept2 ON Depts.PID= Dept2.DeptNumber where 报修任务表.Flag= '" + Flag + "'"; ;



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;




        }


        public DataSet RPlist_UnFinish()
        {

            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();



            string selectStr = "select Dept2.Name,Depts.Name,设备表.设备名称,报修任务表.维修进程,User2.ChineseName,报修任务表.报修人,报修任务表.报修时间,报修任务表.Flag,报修任务表.故障描述 FROM  报修任务表  JOIN Depts  ON 报修任务表.所属科室=Depts.DeptNumber JOIN Depts Dept2 ON  Depts.PID= Dept2.DeptNumber JOIN Users as User2  ON  报修任务表.执行人=User2.ID  JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag   WHERE  报修任务表.维修进程!= '任务完成' AND 报修任务表.维修进程!= '等待回应' order by 报修任务表.ID desc";


            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  



            con.Close();

            return ds;

        }
        public DataSet Task_Image_List(string Flag)
        {

            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select * FROM  报修任务文件表 where 报修任务文件表.Flag= '" + Flag + "'"; ;




            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;

        }
        public DataSet Task_Log_List(string Flag)
        {

            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select 记录内容,记录人,记录时间 FROM  报修任务记录表 where 报修任务记录表.Task_Flag= '" + Flag + "'";





            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;


        }


        public string Change_Task_State(string TaskFlag, string State)
        {

            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "update 报修任务表 set 维修进程 ='" + State + "' where Flag='" + TaskFlag + "'";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();




            string selectStr2 = "select 报修人微信UserID from 报修任务表 where Flag='" + TaskFlag + "'";
            DataSet ds = new DataSet();
            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr2, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                string id = ds.Tables[0].Rows[0][0].ToString();
                if (id.Length > 2)
                {
                    //SendMSG sd = new SendMSG();
                   // sd.Send(id, "您的报修任务状态已更新：" + State);
                }

            }
            return "ok";


        }

        public DataSet EGN_List()
        {


            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select ID,ChineseName,WeChat from Users  where Remark= '工程师' or Remark= '总工程师'";




            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;


        }
        public string Change_Task_HSPEGN(string TaskFlag, string HSPEGNID)
        {




            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "update 报修任务表 set 执行人 ='" + HSPEGNID + "' where Flag='" + TaskFlag + "'";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "ok";

        }
        public DataSet LogInfo(string Flag)
        {



            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select * FROM 报修任务记录表 where 报修任务记录表.Flag= '" + Flag + "'";




            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;




        }

        public DataSet Log_Image_List(string Flag)
        {



            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select * FROM  报修任务记录文件表 where 报修任务记录文件表.Flag= '" + Flag + "'";




            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;



        }

        public DataSet EQ_List(string DeptNumber)
        {

            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select Flag,设备名称,设备型号,生产厂商,出厂编号,存放位置  from 设备表 where 所属科室= '" + DeptNumber + "'";



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;

        }


        public DataSet RPlist_Unapprove()
        {
           

            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select 报修任务表.ID,报修任务表.Flag,报修任务表.设备Flag,报修任务表.故障描述,报修任务表.报修时间,报修任务表.维修进程,报修任务表.报修人,报修任务表.报修人,报修任务表.报修人,报修任务表.执行人,Users2.Name,Users2.ChineseName,Depts.Name,设备表.设备名称,报修任务表.审批意见,报修任务表.审批附言,Users3.ChineseName,报修任务表.市场价格,报修任务表.维修预算,报修任务表.预算附言 FROM  报修任务表  JOIN 设备表  ON  报修任务表.设备Flag=设备表.Flag JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber LEFT  JOIN  Users as Users2  ON  Users2.ID=报修任务表.申请人 LEFT  JOIN  Users as Users3  ON  Users3.ID=报修任务表.审批人     WHERE  (报修任务表.维修进程!= '任务完成' and 报修任务表.审批意见!= '无需审批') order by 报修任务表.ID desc";



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;

        }



        public DataSet RPlist_UnProv_Top5()
     
        {
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();
           


            DataSet ds = new DataSet();
            DataSet dstemp = new DataSet();
        


            string selectStr = "select  报修任务表.ID,报修任务表.Flag,报修任务表.设备Flag,报修任务表.故障描述,报修任务表.报修时间,报修任务表.维修进程,报修任务表.执行人,报修任务表.执行人,Depts.Name,设备表.设备名称,报修任务表.审批意见 FROM  报修任务表  JOIN 设备表  ON  报修任务表.设备Flag=设备表.Flag JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber     WHERE 报修任务表.审批意见!= '同意' order by 报修任务表.ID desc LIMIT 5";



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();








            return ds;






        }






        public DataSet EQ_Task_List(string EQFlag)
        {


            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select 设备表.设备名称,报修任务表.故障描述,报修任务表.报修时间,User2.ChineseName,报修任务表.维修进程,User2.WeChat,报修任务表.Flag FROM  报修任务表  JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber JOIN Users as User2  ON  报修任务表.执行人=User2.ID  JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag   WHERE  报修任务表.设备Flag='" + EQFlag + "' order by 报修任务表.ID desc";



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  

            con.Close();
            ds.Tables[0].Columns[3].ColumnName = "执行人";
            return ds;

        }

        public DataSet RPlist_Caller(string username)
        {



            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();



            string selectStr = "select 设备表.设备名称,报修任务表.故障描述,报修任务表.报修时间,User2.ChineseName,报修任务表.维修进程,User2.WeChat,报修任务表.Flag FROM  报修任务表  JOIN Depts  ON 报修任务表.所属科室=Depts.DeptNumber  JOIN Users as User2  ON  报修任务表.执行人=User2.ID JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag)   WHERE  报修任务表.报修人电话='" + username + "' and 报修任务表.维修进程!= '任务完成' order by 报修任务表.ID desc";



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  

            con.Close();
            ds.Tables[0].Columns[3].ColumnName = "执行人";
            return ds;


        }

        public string Add_Task_Log_File(string FileName, string Title, string Content, string ACCESS_TOKEN)
        {

            string fm = GetMultimedia(ACCESS_TOKEN, FileName);
            fm = fm.Replace("D:\\LeonImage\\", "");


            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string sql2 = "insert into Image (FileName,Type,Date,Title,Describe) values('" + fm + "','jpg','" + Get_DateTime() + "','" + Title + "','" + Content + "')";

            string sql = "insert into Image (FileName,Type,Date,Title,ct) values('" + fm + "','jpg','" + Get_DateTime() + "','" + Title + "','" + Content + "')";
            MySqlCommand mysqlcom = new MySqlCommand(sql, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();

            return "1";



        }

        public string Add_Task_Log(string TaskFlag, string Content, string UserName)
        {


            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string Flag = Guid.NewGuid().ToString();
            string sql = "insert into 报修任务记录表(Flag,Task_Flag,记录内容,记录人,记录时间) values('" + Flag + "','" + TaskFlag + "','" + Content + "','" + UserName + "','" + Get_DateTime() + "')";
            MySqlCommand mysqlcom = new MySqlCommand(sql, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();

            return "1";




        }


        public string Add_Task(string Flag, string EQFlag, string Describe, string FaultCode, string CallPerson, string CallTime, string AnswerTime, string TaskPersonID, string Progress, string PictureCount, string VoiceCount, string VideoCount, string Department, string Hospital, string Type, string Repair_Cost, string CallPhone, string userid)
        {






            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string sql = "insert into 报修任务表 (Flag,设备Flag,故障描述,故障代码,报修人,报修时间,回应时间,执行人,维修进程,图片数量,音频数量,视频数量,所属科室,所属医院,任务类别,维修费,生成日期,报修人电话,报修人微信UserID) values('" + Flag + "','" + EQFlag + "','" + Describe + "','" + FaultCode + "','" + CallPerson + "','" + CallTime + "','" + AnswerTime + "','" + TaskPersonID + "','" + Progress + "','" + PictureCount + "','" + VoiceCount + "','" + VideoCount + "','" + Department + "','" + Hospital + "','" + Type + "','" + Repair_Cost + "','" + Get_DateTime() + "','" + CallPhone + "','" + userid + "')";
            MySqlCommand mysqlcom = new MySqlCommand(sql, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();

            return "1";
        }
        public DataSet Apply_List_Chief()
        {



            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "select 设备表.设备名称,报修任务表.故障描述,报修任务表.维修进程,报修任务表.报修人,报修任务表.Flag,报修任务表.申请人,报修任务表.市场价格 as 市场价格,报修任务表.维修预算 as 维修预算,报修任务表.预算附言,报修任务表.Flag FROM  报修任务表  JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber JOIN Users as User2  ON  报修任务表.执行人=User2.ID  JOIN 设备表  ON  设备表.Flag=报修任务表.设备Flag   WHERE  报修任务表.审批意见!='同意'  and  报修任务表.审批意见!='无需审批' order by 报修任务表.ID desc";





            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;


        }
        public string DEL_Log_By_Flag(string Flag)
        {




            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "delete from 报修任务记录表 where Flag='" + Flag + "'";


            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();


            return "1";




        }

        public string DEL_Task_By_Flag(string Flag)
        {
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string selectStr = "delete from 报修任务表 where Flag='" + Flag + "'";


            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();


            return "1";



        }

        public string Update_Log(string id, string Title, string cc)
        {




            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string sql = "update Image set Title='" + Title + "', ct='" + cc + "'  where ID='" + id + "'";


            MySqlCommand mysqlcom = new MySqlCommand(sql, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();


            return "1";


        }
        private void MakeThumbnail(string sourcePath, string newPath, int width, int height)
        {
            System.Drawing.Image ig = System.Drawing.Image.FromFile(sourcePath);
            int towidth = width;
            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = ig.Width;
            int oh = ig.Height;
            if ((double)ig.Width / (double)ig.Height > (double)towidth / (double)toheight)
            {
                oh = ig.Height;
                ow = ig.Height * towidth / toheight;
                y = 0;
                x = (ig.Width - ow) / 2;

            }
            else
            {
                ow = ig.Width;
                oh = ig.Width * height / towidth;
                x = 0;
                y = (ig.Height - oh) / 2;
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(System.Drawing.Color.Transparent);
            g.DrawImage(ig, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
            try
            {
                bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ig.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }

        }

        public string GetMultimedia(string ACCESS_TOKEN, string MEDIA_ID)
        {
            string file = string.Empty;
            string content = string.Empty;
            string strpath = string.Empty;
            string savepath = string.Empty;
            string stUrl = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=" + ACCESS_TOKEN + "&media_id=" + MEDIA_ID;

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);

            req.Method = "GET";
            string newfile;
            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

                strpath = myResponse.ResponseUri.ToString();
                //WriteLog("接收类别://" + myResponse.ContentType);
                WebClient mywebclient = new WebClient();
                savepath = System.Web.HttpContext.Current.Server.MapPath("LeonImage") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + ".jpg";
                // WriteLog("路径://" + savepath);
                try
                {
                    mywebclient.DownloadFile(strpath, savepath);
                    file = savepath;
                }
                catch (Exception ex)
                {
                    savepath = ex.ToString();
                }
                newfile = file.Replace("D:\\LeonImage\\", "");

            }
            MakeThumbnail(file, "D:\\LeonImage\\small_" + newfile, 250, 250);
            return file;
        }
        public DataSet Message_List_Top_5()
        {

            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            DataSet ds = new DataSet();

            string selectStr = "select * from 通知表  order by ID desc LIMIT 5";

            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  


            con.Close();

            return ds;


        }
        public DataSet RPlist_All_Win()
        {
           

            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            DataSet ds = new DataSet();

            string selectStr = "select 报修任务表.ID,报修任务表.Flag,报修任务表.设备Flag,报修任务表.故障描述,报修任务表.报修时间,报修任务表.维修进程,报修任务表.报修人,报修任务表.报修人,报修任务表.报修人,报修任务表.执行人,Users2.Name,Users2.ChineseName,Depts.Name,设备表.设备名称,报修任务表.审批意见,报修任务表.审批附言,Users3.ChineseName,报修任务表.市场价格,报修任务表.维修预算,报修任务表.预算附言 FROM  报修任务表  JOIN 设备表  ON  报修任务表.设备Flag=设备表.Flag JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber  LEFT  JOIN  Users as Users2  ON  Users2.ID=报修任务表.执行人 LEFT  JOIN  Users as Users3  ON  Users3.ID=报修任务表.审批人 order by 报修任务表.ID desc Limit 50";

            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  


            con.Close();

            return ds;


        }
        public DataSet RPlist_Top5()
        {
           


            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            DataSet ds = new DataSet();
            DataSet dstemp = new DataSet();
            string selectStr = "select  报修任务表.ID,报修任务表.Flag,报修任务表.设备Flag,报修任务表.故障描述,报修任务表.报修时间,报修任务表.维修进程,报修任务表.执行人,报修任务表.执行人,Depts.Name,报修任务表.报修人,报修任务表.报修人,设备表.设备名称 FROM  报修任务表  JOIN 设备表  ON  报修任务表.设备Flag=设备表.Flag JOIN Depts  ON  报修任务表.所属科室=Depts.DeptNumber    order by 报修任务表.ID desc LIMIT 5";

            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  


            if (ds.Tables[0].Rows.Count > 0)
            {

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i][6].ToString().Trim() == "无")
                    {
                        ds.Tables[0].Rows[i][6] = "0";
                        ds.Tables[0].Rows[i][7] = "暂无";
                    }
                    else
                    {
                        string selectStr2 = "select Name,ChineseName from Users where ID= '" + ds.Tables[0].Rows[i][6].ToString().Trim() + "'";
                         myadp = new MySqlDataAdapter(selectStr2, con); //适配器   
                         myadp.Fill(dstemp); //将查询到数据填充到数据集  


                        ds.Tables[0].Rows[i][6] = dstemp.Tables[0].Rows[0][0].ToString();
                        ds.Tables[0].Rows[i][7] = dstemp.Tables[0].Rows[0][1].ToString();
                    }

                }

                ds.Tables[0].Columns[6].ColumnName = "UserName";
                ds.Tables[0].Columns[7].ColumnName = "UserName2";
                ds.AcceptChanges();



            }


            con.Close();

            return ds;


        }
        public DataSet Image_List()
        {

            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();



            DataSet ds = new DataSet();

            string selectStr2 = "select  * FROM  Image order by id desc";

            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr2, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  


            con.Close();

            return ds;




        }


    }
}
